<?php

namespace App\Admin\Controllers;

use App\Models\LiveProgram;

use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Facades\Admin;
use Encore\Admin\Layout\Content;
use App\Http\Controllers\Controller;
use Encore\Admin\Controllers\ModelForm;
use App\Models\Channel;
use App\Models\Wiki;
use App\Models\Program;

class LiveProgramController extends Controller
{
    use ModelForm;

    /**
     * Index interface.
     *
     * @return Content
     */
    public function index()
    {
        return Admin::content(function (Content $content) {

            $content->header('当前正播节目');
            $content->description('&nbsp;');

            $content->body($this->grid());
        });
    }

    /**
     * Edit interface.
     *
     * @param $id
     * @return Content
     */
    public function edit($id)
    {
        return Admin::content(function (Content $content) use ($id) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form()->edit($id));
        });
    }

    /**
     * Create interface.
     *
     * @return Content
     */
    public function create()
    {
        return Admin::content(function (Content $content) {

            $content->header('header');
            $content->description('description');

            $content->body($this->form());
        });
    }

    /**
     * Make a grid builder.
     *
     * @return Grid
     */
    protected function grid()
    {
        return Admin::grid(LiveProgram::class, function (Grid $grid) {

            $grid->model()->orderBy('hot', 'desc');
            $grid->column('channel_code', '频道')->display(function($channel_code){
                return Channel::getOneByCode($channel_code)->name;
            });
            $grid->column('program_name', '节目名称');
            $grid->column('hot', '热度');
            $grid->column('wiki_title', '维基');
            $grid->column('tags', 'Tags')->display(function($tags) {
                return $tags ? implode(',', array_slice($tags, 0, 3)) : "";
            });
            //$grid->created_at();
            $grid->updated_at('最后更新');
            $grid->disableExport();
            $grid->disableCreation();

            $grid->filter(function($filter) {
                $filter->disableIdFilter();
                //$filter->where(function ($query) {
                //    $this->id = 'tags';
                //    return $query->where('tags', $this->input);
                //}, '分类');
                $filter->is("tags", '标签');
                $filter->is('channel_code', '频道')->select(Channel::getAllPluck());
                $filter->like('program_name', '节目名称');
            });
        });
    }

    /**
     * Make a form builder.
     *
     * @return Form
     */
    protected function form()
    {
        return Admin::form(LiveProgram::class, function (Form $form) {

            $form->display('id', 'ID');

            $form->display('created_at', 'Created At');
            $form->display('updated_at', 'Updated At');
        });
    }
}
